<template>
  <div class="app-container">
    <el-row :gutter="10">
      <el-col :span="24" :xs="24">
        <el-form
          ref="queryForm"
          :model="queryParams"
          :inline="true"
          label-width="120px"
        >
          <el-form-item label="入住信息单编号" prop="checkInformationNo">
            <el-input
              v-model="queryParams.checkInformationNo"
              placeholder="请输入入住信息单编号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="房间号" prop="roomNo">
            <el-input
              v-model="queryParams.roomNo"
              placeholder="请输入房间号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
            >搜索</el-button>
            <el-button
              icon="el-icon-refresh"
              size="mini"
              @click="resetQuery"
            >重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="3">
            <el-button
              type="primary"
              icon="el-icon-shopping-cart-full"
              size="medium"
              :disabled="multiple"
              @click="handlePay"
            >结账</el-button>
          </el-col>
        </el-row>

        <el-row :gutter="10">
          <el-col :span="24" :xs="24">
            <el-tabs v-model="activeTabName" @tab-click="handleTabClick">
              <el-tab-pane label="入住信息单管理" name="first">
                <el-table v-loading="loading" :data="orderList" :default-sort="{prop: 'checkInformationId', order: 'descending'}" @selection-change="handleSelectionChange">
                  <!-- 多选框 -->
                  <el-table-column
                    type="selection"
                    width="55"
                  />
                  <el-table-column label="入住信息单id" align="center" prop="checkInformationId" />
                  <el-table-column label="入住信息单编号" align="center" prop="checkInformationNo" />
                  <el-table-column label="入住起始时间" align="center" prop="startTime"><span slot-scope="scope">{{ dateFormat('YYYY-mm-dd HH:MM',scope.row.startTime) }}</span></el-table-column>
                  <el-table-column label="入住人数" align="center" prop="peopleNumber" />
                  <el-table-column label="房间号" align="center" prop="roomNo" />
                  <el-table-column label="总金额" align="center" prop="totalAmount" />
                  <el-table-column label="顾客类别" align="center" prop="customerType" />
                  <el-table-column label="支付状态" align="center" prop="payStatus">
                    <!-- 使用tag改变颜色 -->
                    <template slot-scope="scope">
                      <el-tag
                        :type="scope.row.payStatus === '未支付' ? 'danger' : 'primary'"
                        disable-transitions
                      >{{ scope.row.payStatus }}</el-tag>
                    </template>
                  </el-table-column>
                  <el-table-column
                    label="操作"
                    align="center"
                    class-name="small-padding fixed-width"
                  >
                    <template slot-scope="scope">
                      <el-button
                        size="small"
                        type="text"
                        icon="el-icon-delete"
                        @click="handleDelete(scope.row)"
                      >删除</el-button>
                    </template>
                  </el-table-column>
                </el-table>
              </el-tab-pane>
              <el-tab-pane label="入住信息明细单" name="second">
                <el-table v-loading="loading" :data="orderItemList">
                  <el-table-column label="入住信息明细单id" align="center" prop="checkInformationDetailsId" />
                  <el-table-column label="房间号" align="center" prop="roomNo" />
                  <el-table-column label="是否含早餐" align="center" prop="breakfast" />
                  <el-table-column label="顾客姓名" align="center" prop="customerName" />
                  <el-table-column label="起始时间" align="center" prop="startTime"><span slot-scope="scope">{{ dateFormat('YYYY-mm-dd HH:MM',scope.row.startTime) }}</span></el-table-column>
                  <el-table-column label="结束时间" align="center" prop="endTime"><span slot-scope="scope">{{ dateFormat('YYYY-mm-dd HH:MM',scope.row.endTime) }}</span></el-table-column>
                  <el-table-column label="入住信息单id" align="center" prop="checkInformationId" />
                  <el-table-column
                    label="操作"
                    align="center"
                    class-name="small-padding fixed-width"
                  >
                    <template slot-scope="scope">
                      <el-button
                        size="small"
                        type="text"
                        icon="el-icon-delete"
                        @click="handledetailDelete(scope.row)"
                      >删除</el-button>
                    </template>
                  </el-table-column>
                </el-table>
              </el-tab-pane>
            </el-tabs>
          </el-col>
        </el-row>

        <el-pagination
          align="center"
          :current-page="pageNum"
          :page-sizes="[5, 10, 50, 100]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />

      </el-col>
    </el-row>
    <!-- 旅客结账框 -->
    <el-dialog :title="title" :visible.sync="openpay" width="500px" append-to-body>

      <el-form ref="paymentform" :model="paymentform" :rules="rules" label-width="auto">
        <!-- <div style="text-align:center"> -->
        <!-- <el-form-item label="id" prop="checkInformationId">
          <el-input
            v-model="paymentform.checkInformationId"
            placeholder="请输入id"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item> -->
        <el-form-item label="id" prop="checkInformationId">
          <el-input v-model="paymentform.checkInformationId" placeholder="请输入id" />
        </el-form-item>
        <el-form-item label="优惠价格" prop="discount">
          <el-input
            v-model="paymentform.discount"
            placeholder="请输入优惠价格（元）"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="收款人" prop="payee">
          <el-input
            v-model="paymentform.payee"
            placeholder="请输入收款人"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="支付方式">
          <el-select v-model="paymentform.paymentMode" placeholder="请选择支付方式">
            <el-option label="银行卡支付" value="银行卡支付" />
            <el-option label="现金支付" value="现金支付" />
            <el-option label="移动支付" value="移动支付" />
          </el-select>
        </el-form-item>
        <!-- <el-button
          type="success"
          icon="el-icon-search"
          size="mini"
          round
          @click="handleQuery"
        >生成账单</el-button> -->
        <el-button
          style="margin-left: 130px"
          type="primary"
          icon="el-icon-money"
          size="small"
          :disabled="multiple"
          round
          @click="payRoomSub"
        >确认结账</el-button>
        <el-button
          type="warning"
          icon="el-icon-delete"
          size="small"
          :disabled="multiple"
          round
          @click="cancelPay"
        >取 消</el-button>
      </el-form>
      <!-- <el-table :data="paymentList">
        <el-table-column label="房间号" align="center" prop="roomNo" />
        <el-table-column label="总金额" align="center" prop="totalAmount" />
        <el-table-column label="入住起始时间" align="center" prop="startTime" />
      </el-table> -->
    </el-dialog>
  </div>
</template>

<script>
import { listOrderByPage, getOrder, addOrder, updateOrder, deleteOrder, delcheckinItem, listOrderItemByPage, addPayment } from '@/api/checkin/ordermanage'
// import { delcheckinItem } from '@/api/type'
export default {
  name: 'Item',
  data() {
    return {
      // 遮罩层
      loading: false,
      // 总条数
      total: 0,
      // 当前页
      pageNum: 0,
      // 每页数据量
      pageSize: 0,
      // 选中数组
      ids: [],
      // 非多个禁用
      multiple: true,
      // 图书表格数据
      orderList: [],
      orderItemList: [],
      activeTabName: 'first',
      // 弹出层标题
      title: '',
      // 商品类型
      categoryOptions: undefined,
      // 是否显示弹出层
      open: false,
      // 是否显示支付弹出层
      openpay: false,
      // 支付表单参数
      paymentform: {
        paymentMode: undefined
      },
      value: '',
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        orderNo: undefined,
        orderId: undefined,
        totalAmount: undefined,
        discount: undefined,
        payee: undefined
      },
      // 表单参数
      form: {},
      rules: {
        orderNo: [
          { required: true, message: '订单编号不能为空', trigger: 'blur' }
        ],
        orderId: [
          { required: true, message: '订单ID不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    dateFormat(fmt, date) {
      let ret = ''
      date = new Date(date)
      const opt = {
		    'Y+': date.getFullYear().toString(), // 年
        'm+': (date.getMonth() + 1).toString(), // 月
			  'd+': date.getDate().toString(), // 日
			  'H+': date.getHours().toString(), // 时
			  'M+': date.getMinutes().toString(), // 分
		    'S+': date.getSeconds().toString() // 秒
			        // 有其他格式化字符需求可以继续添加，必须转化成字符串
      }
			 for (const k in opt) {
			   ret = new RegExp('(' + k + ')').exec(fmt)
			        if (ret) {
			    fmt = fmt.replace(
			    ret[1],
			    ret[1].length === 1 ? opt[k] : opt[k].padStart(ret[1].length, '0')
			      )
			    }
			  }
			  return fmt
			    },
    /** 查询商品列表 */
    getList() {
      this.loading = true
      listOrderByPage(this.queryParams).then((response) => {
        // api接口数据
        this.orderList = response.data.list
        this.total = response.data.total
        this.pageNum = response.data.pageNum
        this.pageSize = response.data.pageSize
        this.loading = false
      })
      this.loading = true
      listOrderItemByPage(this.queryParams).then((response) => {
        this.orderItemList = response.data.list
        this.total = response.data.total
        this.pageNum = response.data.pageNum
        this.pageSize = response.data.pageSize
        this.loading = false
      })
    },
    /** 处理分页 */
    handleSizeChange(val) {
      this.queryParams.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.queryParams.pageNum = val
      this.getList()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.checkInformationId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 取消按钮 */
    cancel() {
      this.open = false
      this.resetForm()
    },
    /** 取消支付按钮 */
    cancelPay() {
      this.openpay = false
      this.resetPayForm()
    },
    /** 表单重置 */
    resetForm() {
      this.form = {
        orderId: undefined,
        orderNo: undefined,
        saleDatetime: undefined,
        totalamount: undefined,
        totalquantity: undefined
      }
    },
    /** 支付表单重置 */
    resetPayForm() {
      this.paymentform = {
        paymentMode: undefined,
        payee: undefined,
        paymentVo: undefined
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
      this.$message.success('查询成功')
    },
    /** 结账界面按钮操作 */
    handlePay(row) {
      this.resetPayForm()
      const postId = row.checkInformationId || this.ids
      getOrder(postId).then((response) => {
        this.paymentform = response.data
        this.openpay = true
        this.title = '顾客结账'
      })
    },
    /** 结账按钮操作 */
    payRoomSub() {
      // this.reset();
      // addPayment(this.paymentform).then((response) => {
      //   this.paymentList = response.data.paymentList
      //   this.$message.success('操作成功')
      //   this.resetForm()
      // })
      // const postId = row.checkInformationId || this.ids
      addPayment(this.paymentform).then(response => {
        this.orderList = response.data
        this.openpay = false
        this.resetPayForm()
        this.$message.success('支付成功')
        this.getList()
      })
      // addType(this.form).then((response) => {
      //         this.openadd = false
      //         this.add = ''
      //         this.fetchData()
      //         // this.$message.success(response.data)
      //         this.$message.success('添加成功')
      //       })
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        orderId: undefined,
        orderNo: undefined,
        saleDatetime: undefined,
        totalamount: undefined,
        totalquantity: undefined
      }
      this.getList()
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.resetForm()
      this.open = true
      this.title = '添加订单'
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.resetForm()
      getOrder(row.orderId).then((response) => {
        this.form = response.data
        this.open = true
        this.title = '修改商品'
      })
    },

    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.title === '修改商品') {
            updateOrder(this.form).then((response) => {
              this.open = false
              this.title = ''
              this.getList()
              this.$message.success(response.data) // 后端返回消息字符串
            })
          } else {
            addOrder(this.form).then((response) => {
              this.open = false
              this.title = ''
              this.getList()
              this.$message.success('添加成功')
            })
          }
        }
      })
    },
    /** 删除入住信息单按钮操作 */
    handleDelete(row) {
      this.$confirm(
        '是否确认删除编号为"' + row.checkInformationNo + '"的数据?',
        '警告',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(function() {
          return deleteOrder(row.checkInformationId)
        })
        .then(() => {
          this.getList()
          this.$message.success('删除成功')
        })
        .catch(() => {})
    },
    /** 删除入住信息明细单按钮操作 */
    handledetailDelete(row) {
      this.$confirm(
        '是否确认删除房间号为"' + row.roomNo + '"的数据项?',
        '警告',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(function() {
          return delcheckinItem(row.checkInformationDetailsId)
        })
        .then(() => {
          this.getList()
          this.$message.success('删除成功')
        })
        .catch(() => {})
    }
  }
}
</script>
<style>
.el-button{
  margin-bottom: 15px;
}
</style>
